﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace StoneStorage
{
    public partial class DeleteInvoiceForm : Form
    {
        private InvoiceKindEnum invoiceKind;
        private StoneStorageDataSet dataSet;
        public MyInvoice Invoice
        {
            get;
            private set;
        }

        public DeleteInvoiceForm(StoneStorageDataSet dSet, InvoiceKindEnum kind)
        {
            if (dSet == null)
            {
                throw new ArgumentNullException("dSet");
            }

            InitializeComponent();
            this.invoiceKind = kind;
            this.dataSet = dSet;

            string s;
            if (kind == InvoiceKindEnum.Income)
            {
                s = " приходной накладной";
            }
            else if (kind == InvoiceKindEnum.Expenditure)
            {
                s = " расходной накладной";
            }
            else
            {
                throw new InvalidOperationException("Не известен тип накладной");
            }

            this.Text += s;

            this.Invoice = new MyInvoice();
            LoadToDgvInvoice();
        }

        private void LoadToDgvInvoice()
        {
            BindingSource bSource = new BindingSource();
            dgvChooseInvoice.DataSource = bSource;
            dgvChooseInvoice.ReadOnly = true;
            dgvChooseInvoice.MultiSelect = false;
            dgvChooseInvoice.AllowUserToAddRows = false;
            dgvChooseInvoice.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgvChooseInvoice.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            if (invoiceKind == InvoiceKindEnum.Income)
            {
                bSource.DataSource = dataSet.IncomeInvoiceData;
                dgvChooseInvoice.Columns[dataSet.IncomeInvoiceData.IdColumn.ColumnName].Visible = false;
                dgvChooseInvoice.Columns[dataSet.IncomeInvoiceData.NumberColumn.ColumnName].HeaderText = "Номер";
                dgvChooseInvoice.Columns[dataSet.IncomeInvoiceData.DateColumn.ColumnName].HeaderText = "Дата";
                dgvChooseInvoice.Columns[dataSet.IncomeInvoiceData.DelivererColumn.ColumnName].HeaderText = "Сдал";
                dgvChooseInvoice.Columns[dataSet.IncomeInvoiceData.AccepterColumn.ColumnName].HeaderText = "Принял";
            }

            if (invoiceKind == InvoiceKindEnum.Expenditure)
            {
                bSource.DataSource = dataSet.ExpenditureInvoiceData;
                dgvChooseInvoice.Columns[dataSet.ExpenditureInvoiceData.IdColumn.ColumnName].Visible = false;
                dgvChooseInvoice.Columns[dataSet.ExpenditureInvoiceData.NumberColumn.ColumnName].HeaderText = "Номер";
                dgvChooseInvoice.Columns[dataSet.ExpenditureInvoiceData.DateColumn.ColumnName].HeaderText = "Дата";
                dgvChooseInvoice.Columns[dataSet.ExpenditureInvoiceData.DelivererColumn.ColumnName].HeaderText = "Сдал";
                dgvChooseInvoice.Columns[dataSet.ExpenditureInvoiceData.AccepterColumn.ColumnName].HeaderText = "Принял";
            }
        }

        private void LoadToDgvShowInvoice()
        {
            var bSourse = new BindingSource();
            bSourse.DataSource = this.Invoice.Items;
            dgvShowInvoice.DataSource = bSourse;
            dgvShowInvoice.ReadOnly = true;
            dgvShowInvoice.MultiSelect = false;
            dgvShowInvoice.AllowUserToAddRows = false;
            dgvShowInvoice.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            dgvShowInvoice.Columns["StoneId"].HeaderText = "Вид камня";
            dgvShowInvoice.Columns["StoneId"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

            dgvShowInvoice.Columns["ProducerId"].HeaderText = "Производитель";
            dgvShowInvoice.Columns["ProducerId"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

            dgvShowInvoice.Columns["Length"].HeaderText = "Длина";
            dgvShowInvoice.Columns["Width"].HeaderText = "Ширина";
            dgvShowInvoice.Columns["Height"].HeaderText = "Высота";
            dgvShowInvoice.Columns["Count"].HeaderText = "Количество";
        }

        private void dgvShowInvoice_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.Value == null)
            {
                return;
            }

            if (this.dgvShowInvoice.Columns[e.ColumnIndex].Name == "StoneId")
            {
                e.Value = dataSet.StoneDictionary.FindById((int)e.Value).Name;
            }

            if (this.dgvShowInvoice.Columns[e.ColumnIndex].Name == "ProducerId")
            {
                e.Value = dataSet.ProducerDictionary.FindById((int)e.Value).Name;
            }

        }

        private void dgvChooseInvoice_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            if (e.StateChanged == DataGridViewElementStates.Selected)
            {
                if (e.Row.Index >= dataSet.IncomeInvoiceData.Rows.Count)
                {
                    return;
                }

                var row = (DataRowView)e.Row.DataBoundItem;

                if (row == null)
                {
                    return;
                }

                if (invoiceKind == InvoiceKindEnum.Income)
                {
                    var invoiceDataRow = (StoneStorageDataSet.IncomeInvoiceDataRow)row.Row;

                    var bRules = new BusinessRules(dataSet);
                    this.Invoice = bRules.CreateMyInvoice(invoiceDataRow, invoiceKind);

                    LoadToDgvShowInvoice();
                }

                if (invoiceKind == InvoiceKindEnum.Expenditure)
                {
                    var invoiceDataRow = (StoneStorageDataSet.ExpenditureInvoiceDataRow)row.Row;

                    var bRules = new BusinessRules(dataSet);
                    this.Invoice = bRules.CreateMyInvoice(invoiceDataRow, invoiceKind);

                    LoadToDgvShowInvoice();
                }

            }

        }

    }
}
